﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace InnovTech.Util.IO
{
    /// <summary>
    /// Represents a utility class to make the operations on the file system easy.
    /// </summary>
    public static class FileSystemUtil
    {
        public static List<string> GetFiles(DirectoryInfo dir, Predicate<string> predicate, bool recursive = false)
        {
            if (!dir.Exists)
                throw new ArgumentNullException("The given directory does not exist");

            List<string> result = new List<string>();
            foreach (var file in dir.GetFiles())
            {
                if (predicate(file.FullName))
                    result.Add(file.FullName);
            }
            if (recursive)
            {
                foreach (var subDir in dir.GetDirectories())
                {
                    result.AddRange(GetFiles(subDir, predicate, recursive));
                }
            }
            return result;
        }

        public static List<string> GetFiles(string dir, Predicate<string> predicate, bool recursive = false)
        {
            if (string.IsNullOrWhiteSpace(dir))
                throw new ArgumentNullException("The given directory can not be null or empty");
            DirectoryInfo dirInfo = new DirectoryInfo(dir);
            return GetFiles(dirInfo, predicate, recursive);
        }

        public static List<string> GetFiles(string dir, bool recursive = false)
        {
            if (string.IsNullOrWhiteSpace(dir))
                throw new ArgumentNullException("The given directory can not be null or empty");
            DirectoryInfo dirInfo = new DirectoryInfo(dir);
            Predicate<string> predicate = str => true;
            return GetFiles(dirInfo, predicate, recursive);
        }
    }
}
